home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / debugger / ddd-1.000 / ddd-1 / ddd-1.4b / ddd / Graph.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-04  |  4.4 KB  |  182 lines

  1. // $Id: Graph.h,v 1.5 1996/01/04 16:25:23 zeller Exp $
  2. // Graph Class
  3.  
  4. // Copyright (C) 1995 Technische Universitaet Braunschweig, Germany.
  5. // Written by Andreas Zeller (zeller@ips.cs.tu-bs.de).
  6. // 
  7. // This file is part of the DDD Library.
  8. // 
  9. // The DDD Library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Library General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. // 
  14. // The DDD Library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. // See the GNU Library General Public License for more details.
  18. // 
  19. // You should have received a copy of the GNU Library General Public
  20. // License along with the DDD Library -- see the file COPYING.LIB.
  21. // If not, write to the Free Software Foundation, Inc.,
  22. // 675 Mass Ave, Cambridge, MA 02139, USA.
  23. // 
  24. // DDD is the data display debugger.
  25. // For details, see the DDD World-Wide-Web page, 
  26. // `http://www.cs.tu-bs.de/softech/ddd/',
  27. // or send a mail to the DDD developers at `ddd@ips.cs.tu-bs.de'.
  28.  
  29. #ifndef _Nora_Graph_h
  30. #define _Nora_Graph_h
  31.  
  32. #ifdef __GNUG__
  33. #pragma interface
  34. #endif
  35.  
  36.  
  37. #include "assert.h"
  38. #include "GraphGC.h"
  39. #include "GraphNode.h"
  40. #include "GraphEdge.h"
  41. #include "Box.h"
  42. #include "TypeInfo.h"
  43.  
  44. class Graph {
  45. public:
  46.     DECLARE_TYPE_INFO
  47.  
  48. private:
  49.     GraphNode *_firstNode;    // circular list (0, if empty)
  50.     GraphEdge *_firstEdge;    // circular list (0, if empty)
  51.  
  52. protected:
  53.     void addNodes(GraphNode* nodes);
  54.     void addEdges(GraphEdge* edges);
  55.     void addUsedEdges(GraphEdge* edges);
  56.  
  57.     void removeNode(GraphNode* node);
  58.     void removeEdge(GraphEdge* edge);
  59.  
  60.     // needed by the Copy-Constructor
  61.     GraphNode *getNode(GraphNode *node, const Graph& graph) const;
  62.     
  63.     Graph(const Graph& graph);
  64.  
  65. public:
  66.     // Constructors
  67.     Graph():
  68.     _firstNode(0), _firstEdge(0)
  69.     {}
  70.  
  71.     // Destructor
  72.     virtual ~Graph();
  73.   
  74.     // Duplication
  75.     virtual Graph *dup() const
  76.     {
  77.     return new Graph (*this);
  78.     }
  79.  
  80.     // Add Graph
  81.     void operator += (const Graph& g)
  82.     {
  83.     Graph *graph = g.dup();
  84.  
  85.     if (graph->_firstNode)
  86.         addNodes(graph->_firstNode);
  87.     if (graph->_firstEdge)
  88.         addEdges(graph->_firstEdge);
  89.  
  90.     graph->_firstNode = 0;
  91.     graph->_firstEdge = 0;
  92.     delete graph;
  93.     }
  94.  
  95.     // Add Node
  96.     void operator += (GraphNode *node)
  97.     {
  98.     assert(node->next == 0);    // node must not be used yet
  99.     node->next = node;        // now it is used
  100.     node->prev = node;
  101.     addNodes(node);
  102.     }
  103.  
  104.     // Add Edge
  105.     void operator += (GraphEdge *edge)
  106.     {
  107.     assert(edge->next == 0);    // edge must not be used yet
  108.     edge->next = edge;        // now it is used
  109.     edge->prev = edge;
  110.     addEdges(edge);
  111.     }
  112.  
  113.     // Remove Node
  114.     void operator -= (GraphNode *node)
  115.     {
  116.     assert(node->next != 0);    // node must be used
  117.     removeNode(node);
  118.     }
  119.  
  120.     // Remove Edge
  121.     void operator -= (GraphEdge *edge)
  122.     {
  123.     assert(edge->next != 0);    // edge must be used
  124.     removeEdge(edge);
  125.     }
  126.  
  127.     // Iteration on all nodes and edges
  128.     // simulate a 0-terminated list
  129.     GraphNode *firstNode() const { return _firstNode; } 
  130.     GraphNode *nextNode(GraphNode *ref) const
  131.     {
  132.     return ref->next == _firstNode ? 0 : ref->next;
  133.     }
  134.  
  135.     GraphEdge *firstEdge() const { return _firstEdge; }
  136.     GraphEdge *nextEdge(GraphEdge *ref) const
  137.     {
  138.     return ref->next == _firstEdge ? 0 : ref->next;
  139.     }
  140.  
  141.     // Drawing
  142.     void draw(Widget w, const BoxRegion& exposed, const GraphGC& gc) const;
  143.     void draw(Widget w, const BoxRegion& exposed) const
  144.     {
  145.     draw(w, exposed, GraphGC());
  146.     }
  147.     void draw(Widget w) const
  148.     {
  149.     draw(w, BoxRegion(BoxPoint(0, 0), BoxSize(INT_MAX, INT_MAX)));
  150.     }
  151.  
  152.     // Printing
  153.     void _print(ostream& os, const GraphGC& gc) const;
  154.     void _printHeader(ostream& os, const GraphGC& gc) const
  155.     {
  156.     Box::_printHeader(os, region(gc), *gc.printGC);
  157.     }
  158.     void _printTrailer(ostream& os, const GraphGC& gc) const
  159.     {
  160.     Box::_printTrailer(os, region(gc), *gc.printGC);
  161.     }
  162.  
  163.     // Custom function
  164.     void print(ostream& os, const GraphGC& gc = GraphGC()) const
  165.     {
  166.     _printHeader(os, gc);
  167.     _print(os, gc);
  168.     _printTrailer(os, gc);
  169.     }
  170.  
  171.     // Total Region
  172.     BoxRegion region(const GraphGC& gc) const;
  173.  
  174.     // representation invariant
  175.     virtual bool OK() const;
  176. };
  177.  
  178. // Echo
  179. extern ostream& operator << (ostream& s, const Graph& g);
  180.  
  181. #endif
  182.